<?php
class filter_listModel extends baseModel{
    protected $table = 'filter_list';

    //属性列表
    public function filter_list($cid) {
        return $this->select('cid='.$cid,'','sequence asc,fid asc');
    }

    //判断重复
    public function filter_info($cid,$urlname,$fid=null) {
        if(!empty($fid)){
            $where=' AND fid<>'.$fid;
        }
        return $this->count('cid='.$cid.' AND urlname="'.$urlname.'"'.$where);
    }

    //属性信息
    public function info($fid) {
        return $this->find('fid='.$fid);
    }

    //添加属性
    public function add($data)
    {

        return $this->insert($data);
    }
    //编辑属性
    public function edit($data)
    {
        $condition['fid']=intval($data['fid']);
        return $this->update($condition,$data); 
    }
    //删除属性
    public function del($fid)
    {
        model('filter')->list_del($fid);
        model('filter_relation')->del_relation($fid);
        return $this->delete('fid='.intval($fid)); 
    }

    //获取完整录入html
    public function get_filter($cid,$aid=null,$layout_html=null)
    {
        $category_info=api('admin','category_info',$cid);
        if(empty($category_info['filter'])){
            return;
        }
        //获取选项列表
        $filter_list=$this->filter_list($category_info['filter']);
        if(empty($filter_list)){
            return;
        }
        if(empty($layout_html)){
            $layout_html='
            <div class="formitm">
              <label class="lab" ><{name}>：</label>
              <div class="ipt">
                <{html}>
              </div>
            </div>
            ';
        }
        $out_html='';
        foreach ($filter_list as $value) {
            //查找选项
            $filter=model('filter')->filter_list($value['fid']);
            if(empty($filter)){
                continue;
            }
            //加载类别
            if($value['type']){
                $type='type="checkbox"';
            }else{
                $type='type="radio"';
            }
            if(!empty($aid)){
                $array=model('filter_relation')->get_content_relation($aid,$value['fid']);
            }
            $body_html='';
            if(!empty($filter)){
                foreach ($filter as $vo) {
                    if(in_array(intval($vo['id']),  (array)$array)){
                        $checked=' checked ';
                    }else{
                        $checked='';
                    }
                    $body_html.='<input name="filter['.$value['fid'].'][]" '.$checked.$type.' value="'.$vo['id'].'" > '.$vo['name'].'&nbsp;&nbsp;';
                }
            }
            $html=str_replace('<{name}>', $value['name'], $layout_html);
            $html=str_replace('<{html}>', $body_html,$html);
            $out_html.=$html;
        }
        return $out_html;
    }

    //内容信息头部html
    public function filter_info_html($info=array())
    {
        $html='<div id="filter_list"></div>';
        return $html;
    }

    //内容栏目选择JS
    public function filter_info_js_class($info=array())
    {
        $html='
        $.post("'.url('filter/admin_list/get_filter').'",{cid: cid, aid: aid},function(data){
            if(!data){
                $("#filter_list").html(\'\');
            }else{
                $("#filter_list").html(data);
            }
        },"html");
        ';
        return $html;
    }

	
}